package com.cold.demo.web;

import cn.hutool.core.io.IoUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.cold.demo.web.config.MyTypeFilter;
import com.cold.demo.web.config.handlemapping.EnableRequestMappingReplace;
import com.cold.demo.web.util.StreamUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.*;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

@ComponentScan(excludeFilters = {
        @ComponentScan.Filter(type = FilterType.REGEX, pattern = {"com\\.cold\\.demo\\.web\\.replacebean\\.ReplacedBean"})
        , @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})

})
@SpringBootApplication
@EnableRequestMappingReplace
@Controller
public class WebApplication {

    public WebApplication() {
        System.out.println();
    }

    public static void main(String[] args) throws Exception {
//        test();

        ConfigurableApplicationContext ctx = SpringApplication.run(WebApplication.class, args);
//        System.out.println();

//        Process dir = Runtime.getRuntime().exec("dir");
//        dir.waitFor();

    }

    @RequestMapping("/test/1;2")
    @ResponseBody
    public Object test(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String             jsonStr     = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
        System.out.println(jsonStr);
        System.out.println("进来了  ");
        return System.currentTimeMillis();
    }

    @RequestMapping("/exec")
    @ResponseBody
    public Object test(String m) throws IOException, InterruptedException {
        String[] env = System.getenv().entrySet().stream().map(en -> en.getKey() + "=" + en.getValue()).collect(Collectors.toList()).toArray(new String[0]);
        Process exec = Runtime.getRuntime().exec(m, env);
        exec.waitFor();
        InputStream inputStream = null;
        try {
            inputStream = exec.getInputStream();
            return StreamUtil.copyToString(inputStream, StandardCharsets.UTF_8);
        } finally {
            IoUtil.close(inputStream);
        }
    }


    private static void test() throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("datetime", "2020-06-19 14:08:19");
        jsonObject.put("pageNumber", "1");
        jsonObject.put("filterDelete", "true");
        jsonObject.put("pageSize", "5");
        String param = jsonObject.toJSONString();

        String RULES         = "0d9bcbd6330049d3aa51274b8922b1fe";
        AES    aes           = SecureUtil.aes(RULES.getBytes());
        String encryptBase64 = aes.encryptBase64(param);
        System.out.println(encryptBase64);
//        System.out.println(aes.decryptStrFromBase64(encryptBase64));

//        String      url   = "http://192.168.0.68:82/dsfa/teas/pub/query/cl";
        String      url = "http://125.69.82.54:11602/dsfa/teas/pub/query/cl";
        HttpRequest get = HttpUtil.createGet(url);
        get.form("_param", encryptBase64);
        System.out.println(get.execute().body());
    }

}
